package entities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import DBside.JDBCConnection.Genres;

import tables.*;

public class CD {
	//private ArrayList<String> id; //TODO: get this id to the class Album
	private Artist albumArtist;
	private String discLength;
	private int revision;
	private Genre genre;
	private HashMap<String, ArrayList<Track>> tracks;
	private Album album;
	
	public CD() {
	this.albumArtist = new Artist(-1, "");
	//this.id = new ArrayList<String>();
	this.tracks = new HashMap<String, ArrayList<Track>>();
	this.discLength = "Unknown";
	this.revision = 0;
	this.album = new Album(-1, "Unknown", -1);
	this.genre = new Genre(-1, "", -1);
	}

	
	//*********** adders: parser functions ********\\
	public void addYear(int year) {
		getAlbum().setYear(year);
	}
	

	public void addId(String freeDBId) {
		this.album.addFreeDBId(freeDBId);
	}
	
	public void addGenreName(String genre) {
		getGenre().setName(genre);
	}
	
	public void addAlbumName(String name) {
		getAlbum().setName(name);
	}
	
	
	//**** getters*****\\
	public HashMap<String, ArrayList<Track>> getTracksMap(){
		return this.tracks;
	}
	
	public ArrayList<Track> getArtist(String artistName){
		return this.tracks.get(artistName.toLowerCase());
	}
	public String getDiscLength() {
		return discLength;
	}
	
	public int getRevision() {
		return revision;
	}
	
	public Album getAlbum(){
		return this.album;
	}
	
	public List<Track> getTracks() {
		//return tracks;
		ArrayList<Track> tracks = new ArrayList<Track>();
		for(List<Track> track : this.tracks.values()){
			tracks.addAll(track);
		}
		return tracks;
	}
	
	public List<String> getId() {
		return this.album.getFreeDBId();
	}
	
	public String[] getAllTracksArtistsString(){
		String[] tracksArtistsString = new String[getTracks().size()];
		for(int i = 0; i < tracksArtistsString.length; i++){
			tracksArtistsString[i] = getTracks().get(i).getArtist().getName();
		}
		return tracksArtistsString;
	}
	
	public ArrayList<Artist> getAllTracksArtists(){
		ArrayList<Artist> tracksArtists = new ArrayList<Artist>();
		for(Track track : getTracks()){
			tracksArtists.add(track.getArtist());
		}
		return tracksArtists;
	}
	
	public ArrayList<Track> getAllTracks(String artistName){
		return getTracksMap().get(artistName.toLowerCase());
	}
	
	public Genre getGenre() {
		return this.genre;
	}

	public Artist getAlbumArtist() {
		return albumArtist;
	}
	
	//**** setters ****\\

	public void setIsInDBToAllArtists(String artistName, Boolean isInDB){
		for(Track track : this.tracks.get(artistName.toLowerCase())){
			track.getArtist().setIsInDB(isInDB);
		}
	}
	
	
	public void setAllArtistsId(String artistName, int id){
		for(Track track : this.tracks.get(artistName.toLowerCase())){
			track.getArtist().setId(id);
		}
	}
	
	public void setAlbumEntityId(int id){
		this.album.setId(id);
	}
	
	public void setDiscLength(String discLength) {
		this.discLength = discLength;
	}

	public void setRevision(int revision) {
		this.revision = revision;
	}
	
	public void setMainGenre(Genres genre){
		getGenre().setparentGenreId(genre.getId());
		
		if(getGenre().getName().equals("")){
			getGenre().setName(genre.getName());
		}
		
	}
	
	//**** tools ****\\
	
	public void addTrack(Track track){
		//this.tracks.add(track);
		ArrayList<Track> trackList = this.tracks.get(track.getArtist().getName().toLowerCase());
		if(trackList == null){
			trackList = new ArrayList<Track>();
			trackList.add(track);
			this.tracks.put(track.getArtist().getName().toLowerCase(), trackList);
		}else{
			trackList.add(track);
		}
	}
	
	public void addArtistToAlbum(Artist artist) {
		this.albumArtist.addArtist(artist);
	}
	
	@Override
	public String toString() {
		return  "ID: " + this.album.getFreeDBId() + "\n" +
				"Album: " + getAlbum().getName() + "\n" +
				//"Main Genre: " + Genres.getGenreFromId(getGenre().getParentGenreId()).getName() + "\n" +
				"Sub Genre: " + getGenre().getName() + "\n" +
				"Album Artist: " + getAlbumArtist().getName() + "\n" +
				"Year: " + getAlbum().getYear() + "\n" +
				"Tracks: " + getTracks().toString() + "\n" ;
	}

	
	public String toStringTest() {
		return  "ID: " + this.album.getFreeDBId() + "\n" +
				"**Album:\n" + getAlbum().toString() + "\n" +
				//"Main Genre: " + Genres.getGenreFromId(getGenre().getParentGenreId()).getName() + "\n" +
				"**Genre:\n" + getGenre().toString() + "\n" +
				"**Album Artist:\n" + getAlbumArtist().toString() + "\n" +
				"***Tracks:\n" + getTracks().toString() + "\n" ;
	}
}













